package com.suitsoft.ut.web.info;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import java.io.File;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;


import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;
import org.springside.modules.utils.web.struts2.Struts2Utils;


import com.suitsoft.ut.commons.Constants;
import com.suitsoft.ut.commons.UserHolder;
import com.suitsoft.ut.dao.HibernateUtils;
import com.suitsoft.ut.entity.info.Info;
import com.suitsoft.ut.service.ServiceException;
import com.suitsoft.ut.service.info.InfoManager;
import com.suitsoft.ut.web.CrudActionSupport;

/**
 * 信息管理Action.
 * 
 * 使用Struts2 convention-plugin annotation定义Action参数.
 * 
 * 
 * @author 
 */
//定义URL映射对应/info/info.action
@Namespace("/info")
//定义名为reload的result重定向到info.action, 其他result则按照convention默认.
@Results( { @Result(name = CrudActionSupport.RELOAD, location = "info.action", type = "redirect") })
public class InfoAction extends CrudActionSupport<Info> {

	private static final long serialVersionUID = 8783878162525847072L;

	private InfoManager infoManager;
	
	
	
	
	/** 上传的文件 */
	private File uploadFile;
	/** 上传文件名 */
	private String uploadFileFileName;
	/** 上传的文件 临时存储 key - 文件名称, value - 文件路径*/
	/** 获取request对象 */
	private HttpServletRequest request = ServletActionContext.getRequest();
	
	/**获取response对象*/
	private HttpServletResponse response = ServletActionContext.getResponse();	

	//-- 页面属性 --//
	private Long id;
	private Info entity;
	private Page<Info> page = new Page<Info>(Constants.PAGE_SIZE);//每页5条记录
	
	@Override
	public String execute() throws Exception {
		List<PropertyFilter> filters = PropertyFilter.buildFromHttpRequest(Struts2Utils.getRequest());
		
		page = infoManager.searchInfo(page, filters);		
		return SUCCESS;
	}
	
	
	
	//-- ModelDriven 与 Preparable函数 --//
	public void setId(Long id) {
		this.id = id;
	}

	public Info getModel() {
		return entity;
	}

	@Override
	protected void prepareModel() throws Exception {
		if (id != null) {
			entity = infoManager.getInfo(id);
		} else {
			entity = new Info();
		}
	}

	//-- CRUD Action 函数 --//
	@Override
	public String list() throws Exception {
		List<PropertyFilter> filters = PropertyFilter.buildFromHttpRequest(Struts2Utils.getRequest());
		//设置默认排序方式
		if (!page.isOrderBySetted()) {
			page.setOrderBy("id");
			page.setOrder(Page.ASC);
		}
		page = infoManager.searchInfo(page, filters);
		return SUCCESS;
	}

	
	@Override
	public String save() throws Exception {
		//String webpath = ServletActionContext.getServletContext().getRealPath("WEB-INF/files");
		String webpath = ServletActionContext.getServletContext().getRealPath("files");
		System.out.println("信息webpath1:"+webpath);
		System.out.println("信息uploadFileFileName:"+uploadFileFileName);
		if (uploadFileFileName != null && uploadFileFileName.length()!=0)
		{
		//获取已经上传的文件信息
		String url = +new Date().getTime() + uploadFileFileName.substring(uploadFileFileName.lastIndexOf("."));
		webpath += "/" +url;
		File newFile = new File(webpath);
		System.out.println("信息webpath2:"+webpath);
		//上传
		uploadFile.renameTo(newFile);
		
		//保存此次上传的文件信息
	//	fileTemp.put(uploadFileFileName, url);	
		
		entity.setInfoAttachmentName(webpath);
		}
		infoManager.saveInfo(entity);
		
		//更新系统运行参数对象
		addActionMessage("保存信息成功");
		return RELOAD;
	}
	
		
	@Override
	public String delete() throws Exception {
		try {
			infoManager.deleteInfo(id);
			addActionMessage("删除信息成功");
		} catch (ServiceException e) {
			logger.error(e.getMessage(), e);
			addActionMessage("删除信息失败");
		}
		return RELOAD;
	}
	


	public void download() throws Exception {
		
		Info info=infoManager.getInfo(id);
		String InfoAttachmentName = info.getInfoAttachmentName();
		 String exportFileName =InfoAttachmentName.substring(InfoAttachmentName.lastIndexOf("/")+1);;
		 System.out.println("exportFileName："+exportFileName);
		 //将数据库中的 相对路径 改为绝对路径
		 //String path = ServletActionContext.getServletContext().getRealPath("/");
		 //path = path + (String) map.get("ATTACHMENT_PATH");
		 String exportFilePath =  InfoAttachmentName.substring(0, InfoAttachmentName.lastIndexOf("/")); 
		 System.out.println("exportFilePath："+exportFilePath);
	
				
			request.setAttribute("exportFilePath", exportFilePath);
			request.setAttribute("exportFileName", exportFileName);
			request.getRequestDispatcher("/info-download.jsp").forward(request, response);
			
	}
	

	//-- 其他Action函数 --//
	/**
	 * 支持使用Jquery.validate Ajax检验用户名是否重复.
	 */
	public String checkInfoTitle() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String newInfoTitle = request.getParameter("infoTitle");
		
	//	if (infoManager.findInfoByTitle(newInfoTitle)) {
	//		Struts2Utils.renderText("true");
	//	} else {
	//		Struts2Utils.renderText("false");
	//	}
		//因为直接输出内容而不经过jsp,因此返回null.
		return null;
	}

	//-- 页面属性访问函数 --//
	/**
	 * list页面显示信息分页列表.
	 */
	public Page<Info> getPage() {
		return page;
	}

	@Override
	public String input() throws Exception {
		// TODO Auto-generated method stub
	
		return INPUT;
	}


   public File getUploadFile() {
		return uploadFile;
	}



	public void setUploadFile(File uploadFile) {
		this.uploadFile = uploadFile;
	}



	public String getUploadFileFileName() {
		return uploadFileFileName;
	}



	public void setUploadFileFileName(String uploadFileFileName) {
		this.uploadFileFileName = uploadFileFileName;
	}



@Autowired
	public void setInfoManager(InfoManager infoManager) {
		this.infoManager = infoManager;
	}




	
}
